package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import com.dhcc.DRGsHosp.application.WardApplication;
import com.dhcc.DRGsHosp.core.domain.Ward;
import com.dhcc.DRGsHosp.facade.WardFacade;
import com.dhcc.DRGsHosp.facade.dto.WardDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.WardAssembler;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

@Named("drgsFacadeImpchl2017051102")
public class WardFacadeImpl implements WardFacade {

	@Inject
	private WardApplication  application;

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	public WardDTO getWard(String wardCode) {
		return WardAssembler.toDTO(application.getWard(wardCode)) ;
	}
	
	public void creatWard(WardDTO wardDTO) {
		application.creatWard(WardAssembler.toEntity(wardDTO));
	}
	
	public void updateWard(WardDTO wardDTO) {
		application.updateWard(WardAssembler.toEntity(wardDTO));
	}
	
	public void removeWard(String wardCode) {
		application.removeWard(application.getWard(wardCode));
	}
	
	public void removeWards(String[] wardCodes) {
		Set<Ward> wards= new HashSet<Ward>();
		for (String wardCode : wardCodes) {
			wards.add(application.getWard(wardCode));
		}
		application.removeWards(wards);
	}
	
	@Override
	public List<WardDTO> findAllWard() {
		return WardAssembler.toDTOs(application.findAllWard());
	}
	
	public Page<WardDTO> pageQueryWard(WardDTO queryVo, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select new Ward(_ward.wardCode,_ward.wardDesc,_ward.wardResume,_department.depCode,_department.depName,_ward.wardOrder) "
	   			+ "from Ward _ward, Department _department "
	   			+ "where 1=1 and _ward.depCode = _department.depCode ");
	   	if (queryVo.getWardDesc() != null && !"".equals(queryVo.getWardDesc())) {
	   		jpql.append(" and _ward.wardDesc like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getWardDesc()));
	   	}
	   	if (queryVo.getDepCode() != null && !"".equals(queryVo.getDepCode())) {
	   		jpql.append(" and _ward.depCode = ?");
	   		conditionVals.add(MessageFormat.format("{0}", queryVo.getDepCode()));
	   	}
	   	jpql.append(" order by _ward.wardOrder ") ;
        @SuppressWarnings("unchecked")
		Page<Ward> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<WardDTO>(pages.getStart(), pages.getResultCount(),pageSize, WardAssembler.toDTOs(pages.getData()));
	}

	@Override
	public List<Map<String,Object>> findWardByDepCode(String[] depCode) {
		String depCodes = StringUtils.convertArrayToQuoteString(depCode) ;
		StringBuffer sql = new StringBuffer( "select ward.ward_code,ward.ward_desc from drgs.t_ward ward \n") ;
		if( depCodes != null) {
			sql.append("where ward.pardep_code in ("+depCodes+") \n" ) ;
		} ;
		sql.append(" order  by ward.ward_order  \n" ) ;
		Map<String,Object> condition = new HashMap<>() ;
		List<Object[]> resultList = application.excuteQuery(sql.toString(), condition) ;
		List<Map<String,Object>> listmap = new ArrayList<>() ;
		if(resultList != null && resultList.size() >0 ) {
			for(Object[] obj : resultList) {
				Map<String,Object> resultMap = new HashMap<>() ;
				resultMap.put("wardCode", obj[0] != null ? (String)obj[0] : "" );
				resultMap.put("wardDesc", obj[1] != null ? (String)obj[1] : "" );
				listmap.add(resultMap) ;
			}
		} 
		return listmap ;
	}

	@Override
	public boolean isWardCodeExist(String wardCode) {
		Ward ward =  application.getWard(wardCode) ;
		if(ward == null ) {
			return false;
		}
		return true;
	}

	@Override
	public Map<String, Object> getDepAndDepGroupByWardCode(String wardCode) {
		Map<String, Object> wardMap = application.getDepAndDepGroupByWardCode(wardCode) ;
		return wardMap;
	}

	@Override
	public boolean isWardOrderExist(String wardOrder,String wardCode) {
		List<Map<String, Object>> list=application.isWardOrderExist(wardOrder);
		if(list.isEmpty()){
			return false;
		}else{
			if((list.get(0).get("WARD_CODE")+"").equals(wardCode)){
				return false;
			}else{
				return true;	
			}
		}
	}
	@Override
	public List<String> getWardNotification() {
		return application.getWardNotification();
	}

	@Override
	public int findMaxWardOrder() {
		return application.findMaxWardOrder();
	}
	
	@Override
	public List<Map<String,Object>> findDepByWardCode(String depGroupCode) {
		StringBuffer sql = new StringBuffer( "select b.dep_code,b.dep_name from drgs.t_ward a left join drgs.t_department b on a.pardep_code=b.dep_code \n") ;
		if( depGroupCode != "") {
			sql.append(" where b.pardepgroup_code='"+depGroupCode+"' \n" ) ;
		} ;
		sql.append(" group by b.dep_code,b.dep_name  \n" ) ;
		Map<String,Object> condition = new HashMap<>() ;
		List<Object[]> resultList = application.excuteQuery(sql.toString(), condition) ;
		List<Map<String,Object>> listmap = new ArrayList<>() ;
		if(resultList != null && resultList.size() >0 ) {
			for(Object[] obj : resultList) {
				Map<String,Object> resultMap = new HashMap<>() ;
				resultMap.put("depCode", obj[0] != null ? (String)obj[0] : "" );
				resultMap.put("depDesc", obj[1] != null ? (String)obj[1] : "" );
				listmap.add(resultMap) ;
			}
		} 
		return listmap ;
	}
	
	@Override
	public List<Map<String,Object>> findDepGroupByDepCode() {
		StringBuffer sql = new StringBuffer( "select c.depgroup_code,c.depgroup_desc from drgs.t_ward a  \n") ;
		sql.append(" left join drgs.t_department b on a.pardep_code=b.dep_code \n" ) ;
		sql.append(" left join drgs.t_depgroup c on b.pardepgroup_code=c.depgroup_code \n" ) ;
		sql.append(" group by c.depgroup_code,c.depgroup_desc \n" ) ;
		Map<String,Object> condition = new HashMap<>() ;
		List<Object[]> resultList = application.excuteQuery(sql.toString(), condition) ;
		List<Map<String,Object>> listmap = new ArrayList<>() ;
		if(resultList != null && resultList.size() >0 ) {
			for(Object[] obj : resultList) {
				Map<String,Object> resultMap = new HashMap<>() ;
				resultMap.put("depGroupCode", obj[0] != null ? (String)obj[0] : "" );
				resultMap.put("depGroupDesc", obj[1] != null ? (String)obj[1] : "" );
				listmap.add(resultMap) ;
			}
		} 
		return listmap ;
	}
	
	
}
